<?php
namespace Admin\Controller;

use Think\Controller;
class UserController extends Controller {
    public function register()
    {
        /***
         * 加载注册页面
         */
        $User = M('User');
        if(IS_POST){
            $data['username'] = trim(I('post.username'));
            if($data['username'] == ''){
                $this->error("请填写用户名");
            }
            //查询数据库里面是否存在此用户
            $user = $User->where($data)->find();
            if ($user) {
                $this->error('此用户名已被注册');
            }
            if(strlen($data['username']) < 5 || strlen($data['username']) >20){
                $this->error("用户名长度不能小于5位并且不能大于20位");
            }
            $data['password'] = set_password(I('post.password'));
            if($data['password'] == ''){
                $this->error("请填写密码");
            }
            /***
             * 验证 两次密码是否一致
             */
            $check_password = I('post.password');
            if(!$check_password = I('post.password')){
                $this->error("两次输入密码不一致");
            }
            $User->add($data);
            $this->success("恭喜你，注册成功",U('User/login'));
        }else{
            $this->display();
        }
    }

    /***
     * 加载登录页面
     */
    public function login()
    {
        $User = M('User');
        if (IS_POST) {
            $data['username'] = trim(I('post.username'));
            $data['password'] = set_password(I('post.password'));

            $code = I('post.code');
            if($code == ''){
                $this->error('请填写验证码');
            }
            if (!$this->check_verify($code)) {
                $this->error('验证码填写错误');
            }

            //查询数据库里面是否存在此用户
            $user = $User->where($data)->find();
            if (!$user) {
                $this->error('用户名或密码输入错误');
            }
            if($_POST['remember']){
                //设置COOKIE到浏览器
                setcookie('token',$user['token'],time()+60*60*24*7,'/');
            }else{
                setcookie('token',$user['token'],'null','/');//销毁COOKIE
            }
//            $_SESSION['user'] = $user;
            $this->success('恭喜您，登录成功', U('Index/index'));
        } else {
            $this->display();
        }
    }
    /***
     * 验证码
     */
    public function verify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'codeSet'     =>    '0123456789'
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }










    /***
     * 安全退出
     */
        public function logout()
    {
        //如果想清空当前用户的session
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), "", time() - 3600, "/");
        }
        session_destroy();
        $this->success('您已成功退出', U('User/login'));
    }
}